#delimit; 
clear all;
set more off;

capture log close;

log using log/wbatus_1021_wb.log, replace;

use work/wbresp_2021.dta, clear;
append using work/wbresp_1013.dta;
merge 1:1 tucaseid tulineno using work/atusresp_0321.dta;
drop _merge;
merge 1:1 tucaseid tulineno using work/atusrost_0321.dta;
drop _merge;
merge 1:1 tucaseid tulineno using work/atuscps_0321.dta;
drop _merge;
merge 1:m tucaseid using work/atusact_0321.dta;
drop _merge;
sort tucaseid tuactivity_n;
merge 1:1 tucaseid tuactivity_n using work/wbact_2021.dta;
rename _merge merge_21;
merge 1:1 tucaseid tuactivity_n using work/wbact_1013.dta, update;
keep if _merge==4 | merge_21==3;
drop _merge merge_21;
codebook tucaseid;

gen male=(sex==1);
gen female=(sex==2);

gen interact=(wuinteract==1);

gen disdrs=(pedisdrs==1);
gen disear=(pedisear==1);
gen diseye=(pediseye==1);
gen disout=(pedisout==1);
gen disphy=(pedisphy==1);
gen disrem=(pedisrem==1);

gen alldis=disdrs+disear+diseye+disout+disphy+disrem;

gen act_dur=tuactdur/60;
gen act_dur2=act_dur*act_dur;
gen start_time=substr(tustarttim,1,2);
destring start_time, replace;

gen excellent=(wegenhth==1);
gen verygood=(wegenhth==2);
gen good=(wegenhth==3);
gen fair=(wegenhth==4);
gen poor=(wegenhth==5);

recode wegenhth (5=1) (4=2) (2=4) (1=5), gen(genhth);

label define genhthl 1 "Poor" 2 "Fair" 3 "Good" 4 "Very good" 5 "Excellent";
label values genhth genhthl;

gen very=(werest==1);
gen somewhat=(werest==2);
gen alittle=(werest==3);
gen notatall=(werest==4);

recode werest (1=4) (2=3) (3=2) (4=1), gen(rest);

recode wetypical (2=0) (3=-1), gen(typical);

gen agesq=age*age;

gen painmd=(wepainmd==1);

gen hbp=(wehbp==1);

gen married=(trsppres==1);
gen partner=(trsppres==2);
gen single=(trsppres==3);

gen elementary=(peeduca<=34);
gen some_high=(peeduca>=35 & peeduca <= 38);
gen high_sch=(peeduca==39);
gen some_col=(peeduca > 39 & peeduca < 43);
gen college=(peeduca == 43);
gen graduate=(peeduca > 43);


gen fincmis=(hefaminc < 0);   /* Only in 2010 */
gen finc1030=(hefaminc>=1 & hefaminc<=8);
gen finc3060=(hefaminc>=9 & hefaminc<=12);
gen finc60100=(hefaminc>=13 & hefaminc<=14);
gen finc100150=(hefaminc==15);
gen finc150=(hefaminc==16);

gen selfemp=(teio1cow==6 | teio1cow==7);
gen employed=((telfs==1 | telfs==2) & selfemp==0);
gen unemployed=(telfs==3 | telfs==4);
gen nlf=(telfs==5);

gen white=(ptdtrace==1 & pehspnon==2);
gen black=(ptdtrace==2 & pehspnon==2);
gen hispanic=(pehspnon==1);
gen other=(ptdtrace>=3 & pehspnon==2);

gen sunday=(tudiaryday==1);
gen monday=(tudiaryday==2);
gen tuesday=(tudiaryday==3);
gen wednesday=(tudiaryday==4);
gen thursday=(tudiaryday==5);
gen friday=(tudiaryday==6);
gen saturday=(tudiaryday==7);

gen holiday=(trholiday==1);

gen msa=(gtmetsta==1);

gen immigrant=(prcitshp==4 | prcitshp==5);

gen athome=(tewhere==1);
gen atwork=(tewhere==2);
gen incar=(tewhere==12 | tewhere==13);
gen inpublic=(athome==0 & atwork==0 & incar==0);


*** wufnactwtp for 2010-13 WB Modules and wufnactwt for 2021 WB Module ***;
gen wactwgt=wufnactwtp;
replace wactwgt=wufnactwt if wactwgt==.;

keep if tuyear>=2013 & tumonth>=3;

tab tuyear;
bysort tucaseid (tuactivity_n): gen act_number=_n;
tab tuyear if act_number==1;

keep if wuhappy>=0 & wumeaning>=0 & wupain>=0 & wusad>=0 & wustress>=0 & wutired>=0;
drop act_number;
bysort tucaseid (tuactivity_n): gen act_number=_n;
tab tuyear wxcantril if act_number==1;
tab tuyear wxcantril;

keep if wxcantril==0;

drop act_number;
bysort tucaseid (tuactivity_n): gen act_number=_n;
tab tuyear wxgenhth if act_number==1;
tab tuyear wxgenhth;
keep if wxgenhth==0;

bysort tucaseid (tuactivity_n): gen act_no=_n;

gen yr2021=(tuyear==2021);


local control1 "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150
                holiday i.tudiaryday i.tumonth i.gestfips";

local control2 "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                holiday i.tudiaryday i.tumonth i.gestfips";

local control3 "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                interact i.trtier1p i.tewhere act_dur i.start_time
				holiday i.tudiaryday i.tumonth i.gestfips";

local control3a "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                interact 
				holiday i.tudiaryday i.tumonth i.gestfips";
				
local control3b "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                i.trtier1p 
				holiday i.tudiaryday i.tumonth i.gestfips";
				
local control3c "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                i.tewhere 
				holiday i.tudiaryday i.tumonth i.gestfips";

local control3d "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                act_dur
				holiday i.tudiaryday i.tumonth i.gestfips";

local control3e "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                i.start_time
				holiday i.tudiaryday i.tumonth i.gestfips";
				
local control3f "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                i.trtier1p i.start_time
				holiday i.tudiaryday i.tumonth i.gestfips";
				
local control4 "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 
				i.gestfips";

local control5 "age agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf trchildnum alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
				i.gestfips";

****** Controls for the Chow tests ******;	 

local icontrol3 "yr2021 c.age c.agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf c.trchildnum c.alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
                interact i.trtier1p i.tewhere c.act_dur i.start_time
				holiday i.tudiaryday i.tumonth i.gestfips";
				
local icontrol5 "yr2021 c.age c.agesq female black hispanic other some_high high_sch some_col college graduate married partner unemployed nlf c.trchildnum c.alldis msa 
                immigrant finc3060 finc60100 finc100150 finc150 hbp painmd i.genhth i.rest
				i.gestfips";
				
				
gen le34=(age<=34);
gen ge35=(age>=35);
gen le44=(age<=44);
gen ge45=(age>=45);

tab tuyear ge45;
tab tuyear ge45 if act_no==1;

******** Table 1 Descriptive Statistics *********;

svyset [pweight=wactwgt];

foreach g in le44 ge45 {;
foreach var in wuhappy wupain wusad wustress wutired wumeaning {;

di "**** `var' `g' ****";
svy: mean `var' if `g'==1, over(yr2021);
test `var'@0.yr2021=`var'@1.yr2021;
};
};

svyset [pweight=wufinlwgt];

foreach g in le44 ge45 {;
foreach var in wecantril age female white black hispanic other elementary some_high high_sch some_col college graduate single married partner employed unemployed nlf trchildnum alldis msa 
               immigrant finc1030 finc3060 finc60100 finc100150 finc150 genhth excellent verygood good fair poor rest very somewhat alittle notatall hbp painmd {;

di "**** `var' `g' ****";
svy: mean `var' if `g'==1 & act_no==1, over(yr2021);
test `var'@0.yr2021=`var'@1.yr2021;
};
};


******* Tables 2 and 3 ********************;
******* Table 2: le44 and controls 1, 2, and 3 are for panels A, B, and C *****;
******* Table 3: ge45 and controls 1, 2, and 3 are for panels A, B, and C *****;

foreach g in le44 ge45 le34 ge35 {;
foreach i in 1 2 3 {;

reg wuhappy yr2021 `control`i'' if `g'==1 [pweight=wactwgt], vce(cluster gestfips);
outreg2 using table/wb_`g'_`i'.doc, word bdec(3) ctitle(wuhappy) title("`g', control `i'") replace;

foreach d in wupain wusad wustress wutired wumeaning {;

reg `d' yr2021 `control`i'' if `g'==1 [pweight=wactwgt], vce(cluster gestfips);
outreg2 using table/wb_`g'_`i'.doc, word bdec(3) ctitle(`d') title("`g', control `i'") append;

};
};
};


****** Table 2: Auxiliary regressions for le44 and panel C *********;

foreach g in le44 {;
foreach i in 3a 3b 3c 3d 3e 3f {;

reg wuhappy yr2021 `control`i'' if `g'==1 [pweight=wactwgt], vce(cluster gestfips);
outreg2 using table/wb_`g'_`i'.doc, word bdec(3) ctitle(wuhappy) title("`g', spec `i'") replace;

foreach d in wupain wusad wustress wutired wumeaning {;

reg `d' yr2021 `control`i'' if `g'==1 [pweight=wactwgt], vce(cluster gestfips);
outreg2 using table/wb_`g'_`i'.doc, word bdec(3) ctitle(`d') title("`g', spec `i'") append;

};
};
};


******* Table 4 Cantril Ladder ********************;
******* Controls 4 and 5 are for panels A and B ********;

foreach i in 4 5 {;

reg wecantril yr2021 `control`i'' if act_no==1 & le44==1 [pweight=wufinlwgt], vce(cluster gestfips);
outreg2 using table/cantril_`i'.doc, word bdec(3) ctitle (le44) title("Cantril, control `i'") replace;

foreach g in ge45 le34 ge35 {;

reg wecantril yr2021 `control`i'' if act_no==1 & `g'==1 [pweight=wufinlwgt], vce(cluster gestfips);
outreg2 using table/cantril_`i'.doc, word bdec(3) ctitle ("`g'") title("Cantril, control `i'") append;
};
};

******** Chow tests of pooling **********;

foreach g in ge45 ge35 {;

forvalues j=3/3 {;
		quietly reg wuhappy (`icontrol`j'')##i.`g' [pweight=wufinlwgt], vce(cluster gestfips);
		quietly testparm (`icontrol`j'')#i.`g' i.`g';
		display as result "Chow test: wuhappy, `g': p-value";
		display as result %6.4f r(p);
		
		foreach dep in wupain wusad wustress wutired wumeaning  {;
		quietly reg `dep' (`icontrol`j'')##i.`g' [pweight=wufinlwgt], vce(cluster tucaseid);
		quietly testparm (`icontrol`j'')#i.`g' i.`g';
		display as result "Chow test: `dep', `g': p-value";
		display as result %6.4f r(p);
		};
	};

forvalues j=5/5 {;
		quietly reg wecantril (`icontrol`j'')##i.`g' [pweight=wufinlwgt], vce(cluster gestfips);
		quietly testparm (`icontrol`j'')#i.`g' i.`g';
		display as result "Chow test: wecantril, `g': p-value";
		display as result %6.4f r(p);
	};
};

log close;